package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.AdInfo;
import com.fx.entity.GoodsInfo;
import com.fx.entity.ShopInfo;
import com.fx.service.AdInfoService;
import com.fx.service.CstInfoService;
import com.fx.service.GoodsInfoService;
import com.fx.service.ShopInfoService;
import com.fx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/shopInfo")
public class ShopInfoController extends BaseController {
    @Autowired
    private ShopInfoService shopInfoService;
    private static String pageTitle = "店铺管理";
    private static String pageUrl = "/shopInfo";
    @Autowired
    private AdInfoService adInfoService;
    @Autowired
    private GoodsInfoService goodsInfoService;
    @Autowired
    private CstInfoService cstInfoService;

    @GetMapping()
    String shopInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "shopInfo/shopInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ShopInfo> shopInfoList = shopInfoService.getList(query);
        int total = shopInfoService.getCount(query);
        return renderPage(total, shopInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "shopInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        ShopInfo shopInfo = shopInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("shopInfo", shopInfo);
        return "shopInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(ShopInfo shopInfo) {
        String id = shopInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = shopInfoService.save(shopInfo);
        } else {
            result = shopInfoService.updateById(shopInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (shopInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/initCombo")
    @ResponseBody
    public List<ShopInfo> initCombo() {
        Map<String, Object> param = new HashMap<String, Object>();
        return shopInfoService.getList(param);
    }

    @GetMapping("/api/getShopInfo/{shopId}")
    @ResponseBody
    public R getShopInfo(@PathVariable("shopId") String shopId, String location) {
        String[] locationArr = location.split("_");
        JSONObject address = LocationUtil.getLocationByLatAndLng(Double.parseDouble(locationArr[0]),Double.parseDouble(locationArr[1]));
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("province", address.getString("province"));
        param.put("city", address.getString("city"));
        param.put("area", address.getString("street"));
        param.put("limit", 4);
        List<AdInfo> adInfoList = adInfoService.getAdByLocation(param);
        if (adInfoList.size() < 4) {
            param.clear();
            param.put("limit", (4 - adInfoList.size()));
            adInfoList.addAll(adInfoService.getAdByLocation(param));
        }
        ShopInfo shopInfo = shopInfoService.getById(shopId);
        String cstId = shopInfo.getFlCstId();
        List<GoodsInfo> goodsInfoList = goodsInfoService.getGoodsByClick(cstId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("shopInfo", shopInfo);
        result.put("goodsList", goodsInfoList);
        result.put("adInfo", adInfoList);
        return R.ok(result);
    }

}
